import BaseComponent from "../../Game/Base/BaseClass/BaseComponent";
import FPJDefine from "../../Game/FPJ/Prefabs/FPJDefine";
const {ccclass, property} = cc._decorator;

@ccclass
export default class DWC_DanMu extends BaseComponent {

    @property(cc.Prefab)
    danMuItem: cc.Prefab = null;

    @property(cc.Node)
    danMuContent:cc.Node = null;

    @property(cc.Float)
    moveMaxTime = 15;

    @property(cc.Float)
    moveMinTime = 10;

    _danMuPool = null;
    _danMuOpen = true;
    _danMuList = [];

    playerEnterMsg = null;

    start () {

        cc.systemEvent.on("DanMuControl",this.danMuControl.bind(this));
        this.playerEnterMsg = cc.vv.eventCustom.on("PlayerEnter",this.playerEnter.bind(this) );

        this.onEvent("ServerNtc");

        //初始化一个弹幕池
        // this._danMuPool = new cc.NodePool();
        let limitCount = 10;
        for(let i = 0 ; i < limitCount ; i++){
            let danMuItem = cc.instantiate(this.danMuItem);
            //this._danMuPool.put(danMuItem);
            danMuItem.setParent(this.danMuContent);
            danMuItem.active = true;
            danMuItem.opacity = 0;
            this._danMuList.push(danMuItem);
        }

        this.initDanMuOpen();
    }

    playerEnter(data){
        this.onATTChatMessageNtc(data[0]);
    }

    initDanMuOpen(){
        let danMuOpen = cc.sys.localStorage.getItem("DanMuOpen");
        if(!danMuOpen){
            cc.sys.localStorage.setItem("DanMuOpen", "1" );
            danMuOpen = "1";
        }
        if(danMuOpen == "1"){
            this._danMuOpen = true;
        }else {
            this._danMuOpen = false;
        }
    }

    protected onDestroy(): void {
        this.offEvent("ServerNtc");
        cc.vv.eventCustom.off("PlayerEnter",this.playerEnterMsg );
        cc.systemEvent.off("DanMuControl",this.danMuControl);
    }

    danMuControl(dmOpen){
        this._danMuOpen = dmOpen;
        if(!this._danMuOpen){
            for(let i in this.danMuContent.children){
                let item = this.danMuContent.children[i];
                item.opacity = 0;
            }
        }
    }

    ServerNtc(data){
        data = data[0];
        let handler_name = "on" + data.cmd;
        if (this[handler_name]) {
            this[handler_name](data);
        }
    }

    onATTChatMessageNtc(data){
        if(this._danMuOpen && FPJDefine.isVipRoom() ){
            this.listenTalkMsg(data);
        }
    }
    //收到聊天消息
    listenTalkMsg(data){

        let danMuItem = this.getDanMuItem();
        let danMuData = data;
        let time = Math.random() * (this.moveMaxTime - this.moveMinTime) + this.moveMinTime ;       //随机时间
        let len = this.danMuContent.width + danMuItem.width;
        danMuItem.getComponent(danMuItem.name).init(danMuData,time,len,this);
        danMuItem.opacity = 255;
        // this.beginMove(danMuItem);

        danMuItem.active = true;


        // danMuItem.runAction(         cc.sequence( cc.moveBy(time,cc.v2(-len,0) ),cc.callFunc(()=>{
        //     this.pushDanMuItem(danMuItem);
        // }) ) )

        // cc.tween(danMuItem)
        //     .by(time,{position:cc.v2(-len,0) } )
        //     .call(()=>{
        //         this.pushDanMuItem(danMuItem);
        //     })
        //     .start();
    }

    beginMove(danMuItem){


    }

    getDanMuItem(){
        let danMuItem = null;
        // if(this._danMuPool.size() > 0){
        //     danMuItem = this._danMuPool.get();
        // }else {
        //     danMuItem = cc.instantiate(this.danMuItem);
        //     danMuItem.setParent(this.danMuContent);
        // }
        if(this._danMuList.length > 0){
            danMuItem = this._danMuList.pop();
        }else {
            danMuItem = cc.instantiate(this.danMuItem);
            danMuItem.setParent(this.danMuContent);
        }
        danMuItem.acitve = true;
        return danMuItem;
    }

    pushDanMuItem(danMuItem){
        //danMuItem.removeFromParent(false);
        danMuItem.active = true;
        danMuItem.opacity = 0;
        // this._danMuPool.put(danMuItem);
        this._danMuList.push(danMuItem);
    }



    //------------------------以下自己測試使用
    btnTest(){
        this.sendmsg(null);
        // UIMgr.open( cc.vv.GLConfig.DWC_FriendList_path )
        // cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");

    }

    //
    sendmsg(callBack){

        let ms = "test";
        let msgData = {};
        msgData["cmd"] = "ATTChat";
        msgData["type"] = 1;
        msgData["content"] = ms;
        msgData["receiver_rid"] = "3288625";

        cc.vv.GLUtils.sendRequest(msgData["cmd"], msgData,  (error, data) => {
            if (data.errcodedes) {
                cc.vv.GLUtils.showTips(data.errcodedes);
                return;
            }else {
                // GCtrl.GLUtils.showTips("发送成功");
            }
        })
    }

}
